home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x14c.c < prev    next >
C/C++ Source or Header  |  1994-08-25  |  9KB  |  402 lines

  1. /* $Id: x14c.c,v 1.11 1994/08/25 04:02:32 mjl Exp $
  2.  * $Log: x14c.c,v $
  3.  * Revision 1.11  1994/08/25  04:02:32  mjl
  4.  * Now can work with either TK or Tcl-DP.  Changed to use TK by default.
  5.  *
  6.  * Revision 1.10  1994/08/10  05:28:50  mjl
  7.  * Ensured that geometry strings are in writable memory as required, and
  8.  * other minor tweaks to improve the demo.
  9.  *
  10.  * Revision 1.9  1994/06/30  17:57:54  mjl
  11.  * All C example programs: made another pass to eliminate warnings when using
  12.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  13.  * (now included by plplot.h), eliminated redundant casts, put in more
  14.  * uniform comments, and other minor changes.
  15.  *
  16.  * Revision 1.8  1994/05/14  05:43:56  mjl
  17.  * Now uses the DP driver and finally works the way I always wanted it to.
  18.  *
  19.  * Revision 1.7  1994/03/30  07:21:58  mjl
  20.  * Changes to all C example programs: special handling for malloc re: header
  21.  * files eliminated, include of stdio.h and stdlib.h eliminated (now done
  22.  * by plplot.h), include of "plplot.h" changed to <plplot.h> to enable
  23.  * simpler builds by the general user, some cleaning up also.
  24. */
  25.  
  26. /*    x14c.c
  27.  
  28.     Demo of multiple stream/window capability (requires Tk or Tcl-DP).
  29.  
  30.     Maurice LeBrun
  31.     IFS, University of Texas at Austin
  32. */
  33.  
  34. #include <plplot.h>
  35.  
  36. #ifndef ROUND
  37. #define ROUND(a)    (PLINT)((a)<0. ? ((a)-.5) : ((a)+.5))
  38. #endif
  39.  
  40. static PLFLT x[101], y[101];
  41. static PLFLT xscale, yscale, xoff, yoff, xs[6], ys[6];
  42. static PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
  43.  
  44. void plot1();
  45. void plot2();
  46. void plot3();
  47. void plot4();
  48. void plot5();
  49.  
  50. /*----------------------------------------------------------------------*\
  51.  * main
  52.  *
  53.  * Plots several simple functions from other example programs.
  54.  *
  55.  * This version sends the output of the first 4 plots (one page) to two
  56.  * independent streams.  
  57. \*----------------------------------------------------------------------*/
  58.  
  59. int
  60. main(void)
  61. {
  62.     int i, digmax;
  63.     int xleng0 = 400, yleng0 = 300, xoff0 = 200, yoff0 = 200;
  64.     int xleng1 = 400, yleng1 = 300, xoff1 = 500, yoff1 = 500;
  65.  
  66. /* Select either TK or DP driver and use a small window */
  67. /* The geometry strings MUST be in writable memory */
  68.  
  69.     char driver[] = "tk";
  70.     char geometry_master[] = "500x410+100+200";
  71.     char geometry_slave[]  = "500x410+650+200";
  72.  
  73.     printf("Demo of multiple output streams via the %s driver.\n", driver);
  74.     printf("Running with the second window as slave.\n");
  75.     printf("\n");
  76.  
  77. /* Set up first stream */
  78.  
  79.     plSetInternalOpt("geometry", geometry_master);
  80.  
  81.     plsdev(driver);
  82.     plssub(2, 2);
  83.     plinit();
  84.  
  85. /* Start next stream */
  86.  
  87.     plsstrm(1);
  88.  
  89. /* Turn off pause to make this a slave (must follow master) */
  90.  
  91.     plSetInternalOpt("geometry", geometry_slave);
  92.     plspause(0);
  93.     plsdev(driver);
  94.     plinit();
  95.  
  96. /* Set up the data & plot */
  97. /* Original case */
  98.  
  99.     plsstrm(0);
  100.  
  101.     xscale = 6.;
  102.     yscale = 1.;
  103.     xoff = 0.;
  104.     yoff = 0.;
  105.     plot1();
  106.  
  107. /* Set up the data & plot */
  108.  
  109.     xscale = 1.;
  110.     yscale = 1.e+6;
  111.     plot1();
  112.  
  113. /* Set up the data & plot */
  114.  
  115.     xscale = 1.;
  116.     yscale = 1.e-6;
  117.     digmax = 2;
  118.     plsyax(digmax, 0);
  119.     plot1();
  120.  
  121. /* Set up the data & plot */
  122.  
  123.     xscale = 1.;
  124.     yscale = 0.0014;
  125.     yoff = 0.0185;
  126.     digmax = 5;
  127.     plsyax(digmax, 0);
  128.     plot1();
  129.  
  130. /* To slave */
  131. /* The pleop() ensures the eop indicator gets lit. */
  132.  
  133.     plsstrm(1);
  134.     plot4();
  135.     pleop();
  136.  
  137. /* Back to master */
  138.  
  139.     plsstrm(0);
  140.     plot2();
  141.     plot3();
  142.  
  143. /* To slave */
  144.  
  145.     plsstrm(1);
  146.     plot5();
  147.     pleop();
  148.  
  149. /* Back to master to wait for user to advance */
  150.  
  151.     plsstrm(0);
  152.     pleop();
  153.  
  154. /* Call plend to finish off. */
  155.  
  156.     plend();
  157.     exit(0);
  158. }
  159.  
  160.  /* =============================================================== */
  161.  
  162. void
  163. plot1(void)
  164. {
  165.     int i;
  166.     PLFLT xmin, xmax, ymin, ymax;
  167.  
  168.     for (i = 0; i < 60; i++) {
  169.     x[i] = xoff + xscale * (i + 1) / 60.0;
  170.     y[i] = yoff + yscale * pow(x[i], 2.);
  171.     }
  172.  
  173.     xmin = x[0];
  174.     xmax = x[59];
  175.     ymin = y[0];
  176.     ymax = y[59];
  177.  
  178.     for (i = 0; i < 6; i++) {
  179.     xs[i] = x[i * 10 + 3];
  180.     ys[i] = y[i * 10 + 3];
  181.     }
  182.  
  183. /* Set up the viewport and window using PLENV. The range in X is */
  184. /* 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are */
  185. /* scaled separately (just = 0), and we just draw a labelled */
  186. /* box (axis = 0). */
  187.  
  188.     plcol(1);
  189.     plenv(xmin, xmax, ymin, ymax, 0, 0);
  190.     plcol(6);
  191.     pllab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2");
  192.  
  193. /* Plot the data points */
  194.  
  195.     plcol(9);
  196.     plpoin(6, xs, ys, 9);
  197.  
  198. /* Draw the line through the data */
  199.  
  200.     plcol(4);
  201.     plline(60, x, y);
  202.     plflush();
  203. }
  204.  
  205.  
  206.  /* =============================================================== */
  207.  
  208. void
  209. plot2(void)
  210. {
  211.     int i;
  212.  
  213. /* Set up the viewport and window using PLENV. The range in X is -2.0 to
  214.        10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
  215.        (just = 0), and we draw a box with axes (axis = 1). */
  216.  
  217.     plcol(1);
  218.     plenv(-2.0, 10.0, -0.4, 1.2, 0, 1);
  219.     plcol(2);
  220.     pllab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function");
  221.  
  222. /* Fill up the arrays */
  223.  
  224.     for (i = 0; i < 100; i++) {
  225.     x[i] = (i - 19.0) / 6.0;
  226.     y[i] = 1.0;
  227.     if (x[i] != 0.0)
  228.         y[i] = sin(x[i]) / x[i];
  229.     }
  230.  
  231. /* Draw the line */
  232.  
  233.     plcol(3);
  234.     plline(100, x, y);
  235.     plflush();
  236. }
  237.  
  238.  /* =============================================================== */
  239.  
  240. void
  241. plot3(void)
  242. {
  243.     int i;
  244.  
  245. /* For the final graph we wish to override the default tick intervals, and
  246.        so do not use PLENV */
  247.  
  248.     pladv(0);
  249.  
  250. /* Use standard viewport, and define X range from 0 to 360 degrees, Y range
  251.        from -1.2 to 1.2. */
  252.  
  253.     plvsta();
  254.     plwind(0.0, 360.0, -1.2, 1.2);
  255.  
  256.     /* Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y. */
  257.  
  258.     plcol(1);
  259.     plbox("bcnst", 60.0, 2, "bcnstv", 0.2, 2);
  260.  
  261.     /* Superimpose a dashed line grid, with 1.5 mm marks and spaces. plstyl
  262.        expects a pointer!! */
  263.  
  264.     plstyl(1, &mark1, &space1);
  265.     plcol(2);
  266.     plbox("g", 30.0, 0, "g", 0.2, 0);
  267.     plstyl(0, &mark0, &space0);
  268.  
  269.     plcol(3);
  270.     pllab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function");
  271.  
  272.     for (i = 0; i < 101; i++) {
  273.     x[i] = 3.6 * i;
  274.     y[i] = sin(x[i] * 3.141592654 / 180.0);
  275.     }
  276.  
  277.     plcol(4);
  278.     plline(101, x, y);
  279.     plflush();
  280. }
  281.  
  282.  /* =============================================================== */
  283.  
  284. void
  285. plot4(void)
  286. {
  287.     int i, j;
  288.     PLFLT dtr, theta, dx, dy, r;
  289.     char text[3];
  290.     PLFLT x0[361], y0[361];
  291.     PLFLT x[361], y[361];
  292.  
  293.     dtr = 3.141592654 / 180.0;
  294.     for (i = 0; i <= 360; i++) {
  295.     x0[i] = cos(dtr * i);
  296.     y0[i] = sin(dtr * i);
  297.     }
  298.  
  299. /* Set up viewport and window, but do not draw box */
  300.  
  301.     plenv(-1.3, 1.3, -1.3, 1.3, 1, -2);
  302.     for (i = 1; i <= 10; i++) {
  303.     for (j = 0; j <= 360; j++) {
  304.         x[j] = 0.1 * i * x0[j];
  305.         y[j] = 0.1 * i * y0[j];
  306.     }
  307.  
  308. /* Draw circles for polar grid */
  309.  
  310.     plline(361, x, y);
  311.     }
  312.  
  313.     plcol(2);
  314.     for (i = 0; i <= 11; i++) {
  315.     theta = 30.0 * i;
  316.     dx = cos(dtr * theta);
  317.     dy = sin(dtr * theta);
  318.  
  319. /* Draw radial spokes for polar grid */
  320.  
  321.     pljoin(0.0, 0.0, dx, dy);
  322.     sprintf(text, "%d", ROUND(theta));
  323.  
  324. /* Write labels for angle */
  325.  
  326.     if (dx >= 0)
  327.         plptex(dx, dy, dx, dy, -0.15, text);
  328.     else
  329.         plptex(dx, dy, -dx, -dy, 1.15, text);
  330.     }
  331.  
  332. /* Draw the graph */
  333.  
  334.     for (i = 0; i <= 360; i++) {
  335.     r = sin(dtr * (5 * i));
  336.     x[i] = x0[i] * r;
  337.     y[i] = y0[i] * r;
  338.     }
  339.     plcol(3);
  340.     plline(361, x, y);
  341.  
  342.     plcol(4);
  343.     plmtex("t", 2.0, 0.5, 0.5,
  344.        "#frPLplot Example 3 - r(#gh)=sin 5#gh");
  345.     plflush();
  346. }
  347.  
  348.  /* =============================================================== */
  349.  
  350. /* Demonstration of contour plotting */
  351.  
  352. #define XPTS      35
  353. #define YPTS      46
  354. #define XSPA      2./(XPTS-1)
  355. #define YSPA      2./(YPTS-1)
  356.  
  357. PLFLT tr[6] =
  358. {XSPA, 0.0, -1.0, 0.0, YSPA, -1.0};
  359.  
  360. void
  361. mypltr(PLFLT x, PLFLT y, PLFLT *tx, PLFLT *ty, void *pltr_data)
  362. {
  363.     *tx = tr[0] * x + tr[1] * y + tr[2];
  364.     *ty = tr[3] * x + tr[4] * y + tr[5];
  365. }
  366.  
  367. static PLFLT clevel[11] =
  368. {-1., -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1.};
  369.  
  370. void
  371. plot5(void)
  372. {
  373.     int i, j;
  374.     PLFLT xx, yy;
  375.     PLFLT **z, **w;
  376.     static PLINT mark = 1500, space = 1500;
  377.  
  378. /* Set up function arrays */
  379.  
  380.     plAlloc2dGrid(&z, XPTS, YPTS);
  381.     plAlloc2dGrid(&w, XPTS, YPTS);
  382.  
  383.     for (i = 0; i < XPTS; i++) {
  384.     xx = (double) (i - (XPTS / 2)) / (double) (XPTS / 2);
  385.     for (j = 0; j < YPTS; j++) {
  386.         yy = (double) (j - (YPTS / 2)) / (double) (YPTS / 2) - 1.0;
  387.         z[i][j] = xx * xx - yy * yy;
  388.         w[i][j] = 2 * xx * yy;
  389.     }
  390.     }
  391.  
  392.     plenv(-1.0, 1.0, -1.0, 1.0, 0, 0);
  393.     plcol(2);
  394.     plcont(z, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11, mypltr, NULL);
  395.     plstyl(1, &mark, &space);
  396.     plcol(3);
  397.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11, mypltr, NULL);
  398.     plcol(1);
  399.     pllab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  400.     plflush();
  401. }
  402.